package com.esd.device.controller;

import com.esd.common.pojo.R;
import com.esd.device.entity.DeviceChannel;
import com.esd.device.entity.vo.DeviceVo;
import com.esd.device.service.DeviceChannelService;
import com.esd.device.utils.NumberUtils;
import com.esd.device.utils.Tcp_Utils_Nio;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("resistance/search")
public class ResistanceSearchController extends BaseOperationController{
    @Autowired
    private Tcp_Utils_Nio tcp_utils;

    @Autowired
    private DeviceChannelService deviceChannelService ;

    @PostMapping("/getDetails")
    public R newGetDetail(@RequestBody DeviceVo deviceVo){
        R r = sendCommandForSearch(deviceVo, "03 FF 11" , tcp_utils) ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(15);
            String[] ss = resultStr.split(" ");
            // ss[0] 通道启用
            String binary = NumberUtils.hexToBinary(ss[0], 4);
            String channelEnabled = "" ;
            channelEnabled = channelEnabled + (binary.charAt(3)== '1'?"启用":"未启用") ;
            channelEnabled = channelEnabled + "," + (binary.charAt(2)== '1'?"启用":"未启用") ;
            if (deviceVo.getDeviceTypeName().equals("新版接地电阻仪（4通道）")){
                channelEnabled = channelEnabled + "," + (binary.charAt(1)== '1'?"启用":"未启用") ;
                channelEnabled = channelEnabled + "," + (binary.charAt(0)== '1'?"启用":"未启用") ;
            }
            int signalChannel = NumberUtils.hexToInt(ss[1]); // 信道

            // ss[2] 通道模式
            String binary1 = NumberUtils.hexToBinary(ss[2], 8);
            String channelMode = "" ;
            if (binary1.substring(6).equals("01")){
                channelMode = channelMode + "手环" ;
            }else if (binary1.substring(6).equals("10")){
                channelMode = channelMode + "台垫" ;
            }else {
                channelMode = channelMode + "设备" ;
            }
            if (binary1.startsWith("01", 4)){
                channelMode = channelMode + "," + "手环" ;
            }else if (binary1.startsWith("10", 4)){
                channelMode = channelMode + "," + "台垫" ;
            }else {
                channelMode = channelMode + "," + "设备" ;
            }
            if (deviceVo.getDeviceTypeName().equals("新版接地电阻仪（4通道）")){
                if (binary1.startsWith("01", 2)){
                    channelMode = channelMode + "," + "手环" ;
                }else if (binary1.startsWith("10", 2)){
                    channelMode = channelMode + "," + "台垫" ;
                }else {
                    channelMode = channelMode + "," + "设备" ;
                }
                if (binary1.startsWith("01")){
                    channelMode = channelMode + "," + "手环" ;
                }else if (binary1.startsWith("10")){
                    channelMode = channelMode + "," + "台垫" ;
                }else {
                    channelMode = channelMode + "," + "设备" ;
                }

            }
            List<DeviceChannel> deviceChannels = deviceChannelService.getListByDeviceId(deviceVo.getDeviceId());
            int int1 = NumberUtils.hexToInt(ss[3] + ss[4]);
            BigDecimal num1 = new BigDecimal(int1).divide(new BigDecimal(10)); // 通道1电阻值

            int int2 = NumberUtils.hexToInt(ss[5] + ss[6]);
            BigDecimal num2 = new BigDecimal(int2).divide(new BigDecimal(10)); // 通道2电阻值

            int int3 = NumberUtils.hexToInt(ss[7] + ss[8]);
            BigDecimal num3 = new BigDecimal(int3).divide(new BigDecimal(10)); // 通道3电阻值

            int int4 = NumberUtils.hexToInt(ss[9] + ss[10]);
            BigDecimal num4 = new BigDecimal(int4).divide(new BigDecimal(10)); // 通道4电阻值
            String numStr ;
            if (deviceChannels.get(0).getChannelType().equals("设备接地")){
                numStr = num1 + "Ω," ;
            }else {
                numStr = num1 + "MΩ," ;
            }
            if (deviceChannels.get(1).getChannelType().equals("设备接地")){
                numStr = numStr + num2 + "Ω" ;
            }else {
                numStr = numStr + num2 + "MΩ" ;
            }
            if (deviceVo.getDeviceTypeName().equals("新版接地电阻仪（4通道）")){
                if (deviceChannels.get(2).getChannelType().equals("设备接地")){
                    numStr = numStr + "," + num3 + "Ω," ;
                }else {
                    numStr = numStr + "," + num3 + "MΩ," ;
                }
                if (deviceChannels.get(3).getChannelType().equals("设备接地")){
                    numStr = numStr + num4 + "Ω" ;
                }else {
                    numStr = numStr + num4 + "MΩ" ;
                }
            }
            // 11-14 各个通道是否有手环插入
            String insertFlag = "" ;
            String flag1 = NumberUtils.hexToBinary(ss[11], 4);
            String flag2 = NumberUtils.hexToBinary(ss[12], 4);
            String flag3 = NumberUtils.hexToBinary(ss[13], 4);
            String flag4 = NumberUtils.hexToBinary(ss[14], 4);

            insertFlag = insertFlag + (flag1.charAt(2)=='1'?"有":"无") ;
            insertFlag = insertFlag + "," + (flag2.charAt(2)=='1'?"有":"无") ;
            if (deviceVo.getDeviceTypeName().equals("新版接地电阻仪（4通道）")){
                insertFlag = insertFlag + "," + (flag3.charAt(2)=='1'?"有":"无") ;
                insertFlag = insertFlag + "," + (flag4.charAt(2)=='1'?"有":"无") ;
            }
            // 15 各个通道比较结果
            String binary2 = NumberUtils.hexToBinary(ss[15], 8);
            String compareResult = "" ;
            if (binary2.substring(6).equals("00")){
                compareResult = compareResult + "正常" ;
            }else if (binary2.substring(6).equals("11")){
                if (flag1.charAt(0)== '1'){
                    compareResult = compareResult + "防呆" ;
                }else {
                    compareResult = compareResult + "待机" ;
                }
            }else{
                compareResult = compareResult + "报警" ;
            }
            if (binary2.startsWith("00", 4)){
                compareResult = compareResult + "," + "正常" ;
            }else if (binary2.startsWith("11", 4)){
                if (flag2.charAt(0)== '1'){
                    compareResult = compareResult + "," + "防呆" ;
                }else {
                    compareResult = compareResult + ","  + "待机" ;
                }
            }else{
                compareResult = compareResult+ ","  + "报警" ;
            }
            if (deviceVo.getDeviceTypeName().equals("新版接地电阻仪（4通道）")){
                if (binary2.startsWith("00", 2)){
                    compareResult = compareResult + "," + "正常" ;
                }else if (binary2.startsWith("11", 2)){
                    if (flag3.charAt(0)== '1'){
                        compareResult = compareResult + "," + "防呆" ;
                    }else {
                        compareResult = compareResult + "," + "待机";
                    }
                }else{
                    compareResult = compareResult  + "," + "报警" ;
                }
                if (binary2.startsWith("00")){
                    compareResult = compareResult  + "," + "正常" ;
                }else if (binary2.startsWith("11")){
                    if (flag4.charAt(0)== '1'){
                        compareResult = compareResult + "," + "防呆" ;
                    }else {
                        compareResult = compareResult + "," + "待机";
                    }
                }else{
                    compareResult = compareResult + "," + "报警" ;
                }
            }
            // 16 地线状态
            String groundStatus = "" ;
            groundStatus = groundStatus + (ss[16].equals("01")?"异常":"正常") ;
            Map<String, Object> result = new HashMap<>() ;
            result.put("signalChannel", signalChannel);
            result.put("channelEnabled" , channelEnabled) ;
            result.put("channelMode" , channelMode) ;
            result.put("numStr" , numStr) ;
            result.put("insertFlag" , insertFlag) ;
            result.put("compareResult" , compareResult) ;
            result.put("groundStatus" , groundStatus) ;
            return R.ok(result);
        }
    }


    @PostMapping("/searchChannelEnabled")
    public R searchChannelEnabled(@RequestBody DeviceVo deviceVo) {
        R r = sendCommandForSearch(deviceVo, "03 09 01" , tcp_utils) ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(18 , 20);
            String binary = NumberUtils.hexToBinary(resultStr, 4);
            int nowNum = NumberUtils.hexToInt(resultStr);
            Map<String, Object> result = new HashMap<>() ;
            boolean chOn1 , chOn2 , chOn3 , chOn4 ;
            chOn1 = (binary.charAt(3)== '1') ;
            chOn2 = (binary.charAt(2)== '1') ;
            chOn3 = (binary.charAt(1)== '1') ;
            chOn4 = (binary.charAt(0)== '1') ;
            result.put("chOn1", chOn1) ;
            result.put("chOn2", chOn2) ;
            result.put("chOn3", chOn3) ;
            result.put("chOn4", chOn4) ;
            result.put("nowNum" , nowNum) ;
            return R.ok(result);
        }
    }

    @PostMapping("/searchChannelType")
    public R searchChannelType(@RequestBody DeviceVo deviceVo) {
        R r = sendCommandForSearch(deviceVo, "03 0A 04" , tcp_utils) ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(18 , 29);
            String[] ss = resultStr.split(" ");
            Map<String, Object> result = new HashMap<>() ;
            result.put("ch1", ss[0]) ;
            result.put("ch2", ss[1]) ;
            result.put("ch3", ss[2]) ;
            result.put("ch4", ss[3]) ;
            return R.ok(result);
        }
    }


    // 查询时间
    @PostMapping("getTime")
    public R getTime(
            @RequestBody DeviceVo deviceVo
    ) {
        R r = sendCommandForSearch(deviceVo, "03 00 06" , tcp_utils) ;
        Map<String, Object> result = new HashMap<>() ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            String resultStr = r.getMsg() ;
            String[] ss = resultStr.split("");
            int year = NumberUtils.hexToInt(ss[6]);
            int month = NumberUtils.hexToInt(ss[7]);
            int day = NumberUtils.hexToInt(ss[8]);
            int hour = NumberUtils.hexToInt(ss[9]);
            int minute = NumberUtils.hexToInt(ss[10]);
            int second = NumberUtils.hexToInt(ss[11]);
            String timeStr = "20";
            if (year < 10) {
                timeStr = timeStr + "0" + year;
            } else {
                timeStr = timeStr + year;
            }
            if (month < 10) {
                timeStr = timeStr + "-0" + month;
            } else {
                timeStr = timeStr + "-" + month;
            }
            if (day < 10) {
                timeStr = timeStr + "-0" + day;
            } else {
                timeStr = timeStr + "-" + day;
            }
            if (hour < 10) {
                timeStr = timeStr + " " + "0" + hour;
            } else {
                timeStr = timeStr + " " + hour;
            }
            if (minute < 10) {
                timeStr = timeStr + ":" + "0" + minute;
            } else {
                timeStr = timeStr + ":" + minute;
            }
            if (second < 10) {
                timeStr = timeStr + ":" + "0" + second;
            } else {
                timeStr = timeStr + ":" + second;
            }
            result.put("message", "获取时间成功，时间为: " + timeStr);
            result.put("data", timeStr);
            return R.ok(result);
        }
    }

    @PostMapping("/getWorkTime/{channelType}")
    public R getWorkTime(
            @RequestBody DeviceVo deviceVo,
            @PathVariable String channelType
    ){
        R r = sendCommandForSearch(deviceVo, "03 " + channelType +" 06" , tcp_utils) ;
        Map<String, Object> result = new HashMap<>() ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            // 03 1C 06 获取3种通道类型的工作时间
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(18);
            String[] ss = resultStr.split(" ");
            Map<String, Object> map = new HashMap<>();

            String timeStart = NumberUtils.hexToIntString(ss[0], 2) + ":"
                    + NumberUtils.hexToIntString(ss[1], 2) + ":"
                    + NumberUtils.hexToIntString(ss[2], 2);

            String timeEnd = NumberUtils.hexToIntString(ss[3], 2) + ":"
                    + NumberUtils.hexToIntString(ss[4], 2) + ":"
                    + NumberUtils.hexToIntString(ss[5], 2);
            map.put("timeStart", timeStart);
            map.put("timeEnd", timeEnd);
            result.put("message", "获取工作时间成功");
            result.put("data", map);
            return R.ok(result);
        }
    }

    // 查询上下限的值
    @PostMapping("getScale/{channelType}")
    public R getUpperAndLower(
            @RequestBody DeviceVo deviceVo ,
            @PathVariable String channelType
    ) {
        R r = sendCommandForSearch(deviceVo, "03 " + channelType +" 04" , tcp_utils) ;
        Map<String, Object> result = new HashMap<>() ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            // 03 1C 0C 获取3种通道类型的上下限数据
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(18);
            String[] ss = resultStr.split(" ");
            Map<String, Object> map = new HashMap<>();
            int upperNum = NumberUtils.hexToInt(ss[0] + ss[1]);
            BigDecimal num1 = new BigDecimal(upperNum).divide(new BigDecimal(10));
            map.put("upperNum", num1);
            int lowerNum = NumberUtils.hexToInt(ss[2] + ss[3]);
            BigDecimal num2 = new BigDecimal(lowerNum).divide(new BigDecimal(10));
            map.put("lowerNum", num2);

            result.put("message", "获取接地电阻参数成功");
            result.put("data", map);
            return R.ok(result);
        }
    }

    // =====旧版接地电阻=====

    // 旧版接地电阻获取工作时间 （三通道有用）
    @PostMapping("old/getWorkTime")
    public R getWorkTime(@RequestBody DeviceVo deviceVo) {
        R r = sendCommandForSearch(deviceVo, "03 12 23" , tcp_utils) ;
        Map<String, Object> result = new HashMap<>() ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(15, 69);
            String[] ss = resultStr.split(" ");

            String timeStr1 = NumberUtils.hexToIntString(ss[0], 2) + ":"
                    + NumberUtils.hexToIntString(ss[1], 2) + ":"
                    + NumberUtils.hexToIntString(ss[2], 2);

            String timeStr11 = NumberUtils.hexToIntString(ss[9], 2) + ":"
                    + NumberUtils.hexToIntString(ss[10], 2) + ":"
                    + NumberUtils.hexToIntString(ss[11], 2);

            String timeStr2 = NumberUtils.hexToIntString(ss[3], 2) + ":"
                    + NumberUtils.hexToIntString(ss[4], 2) + ":"
                    + NumberUtils.hexToIntString(ss[5], 2);
            String timeStr21 = NumberUtils.hexToIntString(ss[12], 2) + ":"
                    + NumberUtils.hexToIntString(ss[13], 2) + ":"
                    + NumberUtils.hexToIntString(ss[14], 2);

            String timeStr3 = NumberUtils.hexToIntString(ss[6], 2) + ":"
                    + NumberUtils.hexToIntString(ss[7], 2) + ":"
                    + NumberUtils.hexToIntString(ss[8], 2);

            String timeStr31 = NumberUtils.hexToIntString(ss[15], 2) + ":"
                    + NumberUtils.hexToIntString(ss[16], 2) + ":"
                    + NumberUtils.hexToIntString(ss[17], 2);

            // 台垫工作时间
            result.put("tdkssj", timeStr1);
            result.put("tdjssj", timeStr11);

            // 终端工作时间
            result.put("sbkssj", timeStr3);
            result.put("sbjssj", timeStr31);

            // 人体工作时间
            result.put("rtkssj", timeStr2);
            result.put("rtjssj", timeStr21);
            return R.ok(result);
        }
    }

    // 双通道和三通道还是有区别的
    // 双通道的终端，两个通道的阀值都是相同的
    @PostMapping("old/getScale")
    public R getScale(@RequestBody DeviceVo deviceVo) {
        R r = sendCommandForSearch(deviceVo, "03 10 11" , tcp_utils) ;
        Map<String, Object> result = new HashMap<>() ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(15, 50);
            String[] ss = resultStr.split(" ");
            BigDecimal num1 = new BigDecimal(NumberUtils.hexToInt(ss[0] + ss[1])).divide(BigDecimal.valueOf(100));
            BigDecimal num2 = new BigDecimal(NumberUtils.hexToInt(ss[2] + ss[3])).divide(BigDecimal.valueOf(100));
            BigDecimal num3 = new BigDecimal(NumberUtils.hexToInt(ss[4] + ss[5])).divide(BigDecimal.valueOf(100));
            BigDecimal num4 = new BigDecimal(NumberUtils.hexToInt(ss[6] + ss[7])).divide(BigDecimal.valueOf(100));
            BigDecimal num5 = new BigDecimal(NumberUtils.hexToInt(ss[8] + ss[9])).divide(BigDecimal.valueOf(100));
            BigDecimal num6 = new BigDecimal(NumberUtils.hexToInt(ss[10] + ss[11])).divide(BigDecimal.valueOf(100));

            result.put("tdsx", num1);
            result.put("tdxx", num2);

            result.put("sbsx", num5);
            result.put("sbxx", num6);

            result.put("rtsx", num3);
            result.put("rtxx", num4);
            return R.ok(result);
        }
    }

    @PostMapping("old/getDetails")
    public R getDetails(
            @RequestBody DeviceVo deviceVo
    ) {
        R r = sendCommandForSearch(deviceVo, "03 11 00" , tcp_utils) ;
        Map<String, Object> result = new HashMap<>() ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            List<DeviceChannel> deviceChannels = deviceChannelService.getListByDeviceId(deviceVo.getDeviceId());
            // 是否是 人体 人体
            boolean isRt1 = deviceChannels.get(0).getChannelType().equals("ESD手环") ;
            boolean isRt2 = deviceChannels.get(1).getChannelType().equals("ESD手环") ;
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(15, 65);
            String[] ss = resultStr.split(" ");
            StringBuilder channelType = new StringBuilder();
            if (deviceVo.getDeviceTypeName().endsWith("接地电阻仪（2通道）")){
                channelType = new StringBuilder(deviceChannels.get(0).getChannelType());
                result.put("channelType" , channelType.toString());
            }else if (deviceVo.getDeviceTypeName().endsWith("接地电阻仪（3通道）")){
                for (int i = 0; i < 3; i++) {
                    String channelTypeName = deviceChannels.get(i).getChannelType();
                    if(i!=0){
                        channelType.append(",");
                    }
                    if (channelTypeName.equals("ESD手环")){
                        channelType.append("人体");
                    }else {
                        channelType.append(channelTypeName, 0, 2);
                    }
                }

                result.put("channelType" , channelType.toString());
            }
            // 时间
            String timeStr = NumberUtils.hexToIntString(ss[0], 2) + ":"
                    + NumberUtils.hexToIntString(ss[1], 2) + ":"
                    + NumberUtils.hexToIntString(ss[2], 2);
            result.put("timeStr", timeStr);
            // 监控开关 （没有什么意义就别解析了）
            // 信道
            int signalChannel = NumberUtils.hexToInt(ss[4]);
            result.put("signalChannel", signalChannel);
            // 查找发出声音开关
            String soundOn = ss[5].equals("00") ? "关闭" : "开启";
            result.put("soundOn", soundOn);
            BigDecimal tdjddz = new BigDecimal(NumberUtils.hexToInt(ss[7] + ss[8])).divide(new BigDecimal(100));
            BigDecimal rtjddz = new BigDecimal(NumberUtils.hexToInt(ss[9] + ss[10])).divide(new BigDecimal(100));
            if (deviceChannels.get(0).getChannelType().equals("设备接地") ){
                result.put("tdjddz", tdjddz);
            }else {
                result.put("tdjddz", tdjddz + "M");
            }
            if (deviceChannels.get(1).getChannelType().equals("设备接地") ){
                result.put("rtjddz", rtjddz);
            }else{
                result.put("rtjddz", rtjddz + "M");
            }
            // 通道三
            BigDecimal sbjddz = new BigDecimal(NumberUtils.hexToInt(ss[11] + ss[12])).divide(new BigDecimal(100));
            result.put("sbjddz", sbjddz);

            // 通道1：有人/手环插入
            if (deviceChannels.get(0).getChannelType().equals("ESD手环")){
                result.put("channel1", ss[13].equals("01")?"是":"否");
            }else {
                result.put("channel1", "否");
            }

            // 通道2：有人/手环插入
            if (isRt1 && isRt2){
                result.put("channel2", ss[14].equals("01")?"是":"否");
            }else{
                result.put("channel2", ss[13].equals("01")?"是":"否");
            }

            // 地线状态
            result.put("dxzt", ss[15].equals("00") ? "正常" : "断开"); // 0 正常 1 断开

            // 比较结果
            String binary = NumberUtils.hexToBinary(ss[16], 8);
            String[] sss = new String[4];
            sss[0] = binary.substring(0, 2);
            sss[1] = binary.substring(2, 4);
            sss[2] = binary.substring(4, 6);
            sss[3] = binary.substring(6);

            BigDecimal compareToNum = new BigDecimal("0.1");
            // 各个通道的比较结果
            switch (sss[0]) {
                case "00":
                    if (isRt1){
                        if (ss[13].equals("01")||ss[13].equals("11")){
                            result.put("channel1Status", "正常");
                        }else if(ss[13].equals("00")){
                            result.put("channel1Status", "待机");
                        }else{
                            result.put("channel1Status", "防呆");
                        }
                    }else{
                        result.put("channel1Status", "正常"); // 正常
                    }
                    break;
                case "01":
                    if (tdjddz.compareTo(compareToNum) <= 0){
                        if (isRt1){
                            if (ss[13].equals("10")){
                                result.put("channel1Status", "防呆");
                            }else{
                                result.put("channel1Status", "待机");
                            }
                        }
                    }else {
                        result.put("channel1Status", "低于下限"); // 低于下限
                    }
                    break;
                case "10":
                    if (tdjddz.compareTo(compareToNum) <= 0){
                        if (isRt1){
                            if (ss[13].equals("10")){
                                result.put("channel1Status", "防呆");
                            }else{
                                result.put("channel1Status", "待机");
                            }
                        }
                    }else {
                        result.put("channel1Status", "高于上限"); // 低于下限
                    }
                    break;
            }

            switch (sss[1]) {
                case "00":
                    if (isRt2){
                        String tempStr2 ;
                        if(isRt1){
                            tempStr2 = ss[14] ;
                        }else{
                            tempStr2 = ss[13] ;
                        }
                        if (tempStr2.equals("01")||tempStr2.equals("11")){
                            result.put("channel2Status", "正常");
                        }else if(tempStr2.equals("00")){
                            result.put("channel2Status", "待机");
                        }else{
                            result.put("channel2Status", "防呆");
                        }
                    }else {
                        result.put("channel2Status", "正常"); // 正常
                    }
                    break;
                case "01":
                    if (rtjddz.compareTo(compareToNum) <= 0){
                        if (isRt1&&isRt2){
                            if (ss[14].equals("10")){
                                result.put("channel2Status", "防呆");
                            }else{
                                result.put("channel2Status", "待机");
                            }
                        }else if ( (!isRt1) && isRt2 ){
                            if (ss[13].equals("10")){
                                result.put("channel2Status", "防呆");
                            }else{
                                result.put("channel2Status", "待机");
                            }
                        }
                    }else{
                        result.put("channel2Status", "低于下限"); // 低于下限
                    }
                    break;
                case "10":
                    if (rtjddz.compareTo(compareToNum) <= 0){
                        if (isRt1&&isRt2){
                            if (ss[14].equals("10")){
                                result.put("channel2Status", "防呆");
                            }else{
                                result.put("channel2Status", "待机");
                            }
                        }else if ( (!isRt1) && isRt2 ){
                            if (ss[13].equals("10")){
                                result.put("channel2Status", "防呆");
                            }else{
                                result.put("channel2Status", "待机");
                            }
                        }
                    }else{
                        result.put("channel2Status", "高于上限"); // 低于下限
                    }
                    break;
            }
            switch (sss[2]) {
                case "00":
                    result.put("channel3Status", "正常"); // 正常
                    break;
                case "01":
                    result.put("channel3Status", "低于下限"); // 低于下限
                    break;
                case "10":
                    result.put("channel3Status", "高于上限"); // 高于上限
                    break;
            }
            result.put("dxStatus", ss[15].equals("00") ? "正常" : "断开");
            return R.ok(result);
        }
    }

    @PostMapping("/checkChOn")
    public R checkChOn(@RequestBody DeviceVo deviceVo){
        int channelNum ;
        if (deviceVo.getDeviceTypeName().contains("2")){
            channelNum = 2 ;
        }else {
            channelNum = 4 ;
        }
        List<DeviceChannel> deviceChannels = deviceChannelService.getListByDeviceId(deviceVo.getDeviceId());
        if (deviceChannels == null || deviceChannels.size() != channelNum){
            return R.error("该终端设置通道数量设置有误！！") ;
        }
        R r = sendCommandForSearch(deviceVo, "03 09 01" , tcp_utils) ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(18 , 20);
            String binary = NumberUtils.hexToBinary(resultStr, 4);
            Map<String, Object> result = new HashMap<>() ;
            boolean chOn1 , chOn2 , chOn3 , chOn4 ;
            chOn1 = (binary.charAt(3)== '1') ;
            chOn2 = (binary.charAt(2)== '1') ;
            chOn3 = (binary.charAt(1)== '1') ;
            chOn4 = (binary.charAt(0)== '1') ;
            deviceChannelService.checkChOn(deviceVo.getDeviceId() , 0 , chOn1) ;
            deviceChannelService.checkChOn(deviceVo.getDeviceId() , 1 , chOn2) ;
            deviceChannelService.checkChOn(deviceVo.getDeviceId() , 2 , chOn3) ;
            deviceChannelService.checkChOn(deviceVo.getDeviceId() , 3 , chOn4) ;
            return R.ok("数据更新成功！！");
        }
    }

    @PostMapping("/checkChTypes")
    public R checkChTypes(@RequestBody DeviceVo deviceVo){
        int channelNum ;
        if (deviceVo.getDeviceTypeName().contains("2")){
            channelNum = 2 ;
        }else {
            channelNum = 4 ;
        }
        List<DeviceChannel> deviceChannels = deviceChannelService.getListByDeviceId(deviceVo.getDeviceId());
        if (deviceChannels == null || deviceChannels.size() != channelNum){
            return R.error("该终端设置通道数量设置有误！！") ;
        }
        R r = sendCommandForSearch(deviceVo, "03 0A 04" , tcp_utils) ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(18 , 29);
            String[] ss = resultStr.split(" ");
            deviceChannelService.checkChTypes(deviceVo.getDeviceId() , 0 , ss[0]) ;
            deviceChannelService.checkChTypes(deviceVo.getDeviceId() , 1 , ss[1]) ;
            deviceChannelService.checkChTypes(deviceVo.getDeviceId() , 2 , ss[2]) ;
            deviceChannelService.checkChTypes(deviceVo.getDeviceId() , 3 , ss[3]) ;
            return R.ok("数据更新成功！！");
        }
    }

    @PostMapping("/checkRedClock")
    public R checkRedClock(@RequestBody DeviceVo deviceVo){
        R r = sendCommandForSearch(deviceVo, "03 FF 11" , tcp_utils) ;
        if (r.getCode() == 500) {
            return  r;
        } else {
            String resultStr = r.getMsg() ;
            resultStr = resultStr.substring(48 , 60);
            if (resultStr.startsWith("1")){
                return R.ok(true) ;
            }
            return R.ok(false);
        }
    }
}
